import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;

/**
 * @author liyanhao
 * @Date 2022-12-02 11:12
 * 非阻塞io
 */
public class RedisServerNIO {
    static ArrayList<SocketChannel> socketChannels = new ArrayList<>();
    static ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

    public static void main(String[] args) throws IOException {
        System.out.println("-----------redisServerNIO 启动等待中----------");
        ServerSocketChannel serverSocket = ServerSocketChannel.open();
        serverSocket.bind(new InetSocketAddress("127.0.0.1", 6379));
        serverSocket.configureBlocking(false);//设置非阻塞模式
        while (true) {
            for (SocketChannel channel : socketChannels) {
                int read = channel.read(byteBuffer);
                if (read > 0) {
                    System.out.println("-----读取数据：-----" + read);
                    byteBuffer.flip();
                    byte[] bytes = new byte[read];
                    byteBuffer.get(bytes);
                    System.out.println(new String(bytes));
                    byteBuffer.clear();
                }
            }
            SocketChannel socketChannel = serverSocket.accept();
            if (socketChannel != null) {
                System.out.println("------成功链接--------");
                socketChannel.configureBlocking(false);
                socketChannels.add(socketChannel);
                System.out.println("-------socketList size:" + socketChannels.size());
            }
        }
    }
}
